<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/3/13
 * Time: 17:26
 */
namespace app\admin\controller;

use \think\Controller;
use \think\Db;

class Apishare extends Common
{
    public function yzm(){
        $phone = input('param.phone');
        header("content-type:image/png");
        $validateLength=4;
        $strToDraw="";
        $chars=[
            "0","1","2","3","4",
            "5","6","7","8","9",
            "a","b","c","d","e","f","g",
            "h","i","j","k","l","m","n",
            "o","p","q","r","s","t",
            "u","v","w","x","y","z",
            "A","B","C","D","E","F","G",
            "H","I","J","K","L","M","N",
            "O","P","Q","R","S","T",
            "U","V","W","X","Y","Z"
        ];
        $imgW=80;
        $imgH=25;
        $imgRes=imagecreate($imgW,$imgH);
        $imgColor=imagecolorallocate($imgRes,255,255,100);
        $color=imagecolorallocate($imgRes,0,0,0);
        for($i=0;$i<$validateLength;$i++){
            $rand=rand(1,58);
            $strToDraw=$strToDraw.$chars[$rand];
        }
        imagestring($imgRes,5,20,5,$strToDraw,$color);
        for($i=0;$i<100;$i++){
            imagesetpixel($imgRes,rand(0,$imgW),rand(0,$imgH),$color);
        }





        $where['phone'] = $phone;
        $res= Db::table('up_phonecode')->where($where)->find();
        if($res['id']!=''){//不等于空的时候
            $data['yzm'] = $strToDraw;
            $data['yzm_status'] = 1;
            
            Db::table('up_phonecode')->where($where)->update($data);
        }else{//等于空空
            $data['yzm'] = $strToDraw;
            $data['phone'] = $phone;
            Db::table('up_phonecode')->insert($data);
        }
        imagepng($imgRes);
        imagedestroy($imgRes);exit;
    }

    public function checkyzm(){

    }
    /**
     * getDogInformation 获取狗的主页信息
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @param $udf_id 狗对应id
     */
    public function getDogInformation(){
        header("Access-Control-Allow-Origin:*");
        extract(input());
        $user_id = 1;

        $data = Db::table('up_dog_family')
            ->alias('udf')
            ->join('up_area u','udf.udf_province=u.district_id','left')
            ->join('up_area ua','udf.udf_city=ua.district_id','left')
            ->join('up_area uaa','udf.udf_district=uaa.district_id','left')
            ->where('udf_id',$udf_id)
            ->field('udf.udf_img,udf.udf_name,udf.udf_sex,udf.udf_age,udf.udf_size,u.district shen,ua.district shi,uaa.district qu,udf.udf_condition,udf.udf_character,udf.udf_character_other,udf.udf_dog_vaccine,udf.udf_dog_outside,udf.udf_dog_inside,udf.udf_dog_sterilization,udf.udf_dog_disability,udf.udf_story')
            ->find();
        $b = Db::table('up_dog_character')
            ->where('uco_id','IN',$data['udf_character'])
            ->column('uco_character');
        $num = explode(',',$data['udf_character']);
        if(in_array(0,$num)){
            $b['other'] = $data['udf_character_other'];
        }
        $data['character'] = $b;
        $num = Db::table('up_u_user_interest_dog')
            ->where(['udf_id'=>$udf_id])
            ->count();
        $data['attention_num'] = $num;
        $atr = Db::table('up_dog_family')
            ->where(['udf_id'=>$udf_id,'user_id'=>$user_id])
            ->find();
        if($atr){
            /*属于我的*/
            $data['belong'] = 1;
        }else{
            /*属于别人的*/
            $data['belong'] = 2;
        }
        $res = Db::table('up_u_user_interest_dog')
            ->where(['user_id'=>$user_id,'udf_id'=>$udf_id])
            ->find();
        if($res){
            /*已关注*/
            $data['attention'] = 1;
        }else{
            /*未关注*/
            $data['attention'] = 2;
        }
        if($data['udf_img']){
            $img = trim($data['udf_img'],',');
            if($img){
                $data['udf_img'] = explode(',',$img);
            }
        }

        return json_encode(['status'=>200,'msg'=>$data]);
    }

    /**
     *getStrayDogDetail  单条流浪狗记录详情
     * @param  $dog_id 流浪狗的id
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return 返回数组
     */
    public function getStrayDogDetail(){
        header("Access-Control-Allow-Origin:*");
        extract(input());
        $data = Db::table('up_dog')
            ->alias('ud')
            ->join('up_u_user uuu','ud.user_id=uuu.user_id','left')
            ->join('up_area u','ud.area_sheng=u.district_id','left')
            ->join('up_area ua','ud.area_shi=ua.district_id','left')
            ->join('up_area uaa','ud.area_qu=uaa.district_id','left')
            ->where(['ud.dog_id'=>$dog_id,'ud.dog_type'=>1,'ud.dog_preserve'=>2])
            ->field('uuu.user_id,uuu.user_username,uuu.user_image,uuu.user_integral,uuu.user_attestation,ud.dog_release_time,ud.dog_img,ud.dog_num,ud.dog_attention_num,ud.dog_status,ud.udsc_id,ud.dog_information,ud.udsc_id_information,ud.dog_add,u.district shen,ua.district shi,uaa.district qu,ud.dog_lat,ud.dog_lon')
            ->find();
        $a = trim($data['udsc_id'],',');
        $b = Db::table('up_dog_strat_characteristic')
            ->where('udsc_id','IN',$a)
            ->column('udsc_name');
        $num = explode(',',$a);
        if(in_array(0,$num)){
            $b['other'] = $data['udsc_id_information'];
        }
        $data['characteristic'] = $b;
        switch ($data['dog_status'])
        {
            case 1:
                $data['dog_status'] = "待救助";
                break;
            case 2:
                $data['dog_status'] = "已救助";
                break;
            default:
                $data['dog_status'] = "失踪";
        }
        if($data['dog_img']){
            $img = trim($data['dog_img'],',');
            $data['dog_img'] = explode(',',$img);
        }
        return json_encode(['status'=>200,'msg'=>$data]);
    }

    /**
     *getHelpEachOtherDetail  单条互助记录详情
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @param  $dog_id 互助狗的id
     */
    public function getHelpEachOtherDetail(){
        header("Access-Control-Allow-Origin:*");
        extract(input());
        $data = Db::table('up_dog')
            ->alias('ud')
            ->join('up_u_user uuu','ud.user_id=uuu.user_id','left')
            ->join('up_dog_family udf','ud.udf_id=udf.udf_id','left')
            ->where(['ud.dog_id'=>$dog_id,'ud.dog_type'=>2,'ud.dog_preserve'=>2])
            ->field('uuu.user_id,uuu.user_username,uuu.user_image,uuu.user_integral,uuu.user_attestation,ud.dog_help_status,udf.udf_name,ud.dog_information,ud.dog_end_time,ud.dog_help_information,ud.dog_release_time,ud.dog_help_style,ud.dog_img,ud.dog_attention_num')
            ->find();
        if($data['dog_img']){
            $img = trim($data['dog_img'],',');
            $data['dog_img'] = explode(',',$img);
        }
        $a = trim($data['dog_help_style'],',');
        $b = Db::table('up_dog_heo')
            ->where('heo_id','IN',$a)
            ->column('heo_name');
        $num = explode(',',$a);
        if(in_array(0,$num)){
            $b['other'] = $data['dog_help_information'];
        }
        $data['heo_category'] = $b;
        switch($data['dog_help_status'])
        {
            case 1:
                $data['dog_help_status'] = "待帮助";
                break;
            case 2:
                $data['dog_help_status'] = "已解决";
                break;
            case 3:
                $data['dog_help_status'] = "暂停帮助";
                break;
            default:
                $data['dog_help_status'] = "已过期";
        }
        return json_encode(['status'=>200,'msg'=>$data]);
    }

    /**
     *getAdoptDetail  单条领养狗记录详情
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @param  $dog_id 领养狗的id
     */
    public function getAdoptDetail(){
        header("Access-Control-Allow-Origin:*");
        extract(input());
        $data = Db::table('up_dog')
            ->alias('ud')
            ->join('up_u_user uuu','ud.user_id=uuu.user_id','left')
            ->join('up_dog_family udf','ud.udf_id=udf.udf_id','left')
            ->field('uuu.user_id,uuu.user_username,uuu.user_image,uuu.user_integral,uuu.user_attestation,ud.dog_release_time,ud.dog_apply_num,ud.dog_attention_num,ud.dog_adopt_status,ud.dog_adopt_experience,ud.dog_adopt_environment,ud.dog_adopt_sterilization,ud.dog_adpot_workers,ud.dog_adpot_protocol,ud.dog_adpot_certificate,ud.dog_adpot_information,ud.dog_information,udf.udf_name,ud.dog_img')
            ->where(['ud.dog_id'=>$dog_id])
            ->find();
        if($data['dog_img']){
            $img = trim($data['dog_img'],',');
            $data['dog_img'] = explode(',',$img);
        }
        switch($data['dog_adopt_status'])
        {
            case 1:
                $data['dog_adopt_status'] = "取消领养";
                break;
            case 2:
                $data['dog_adopt_status'] = "待领养";
                break;
            case 3:
                $data['dog_adopt_status'] = "完成领养";
                break;
            default:
                $data['dog_adopt_status'] = "重新领养";
        }
        return json_encode(['status'=>200,'msg'=>$data]);
    }

    /**
     *getSeekDogDataDetail 获取寻狗详情
     * @param $dog_id狗id
     * @param $user_id 用户id
     * @param $time 时间
     * @param $value 加密值
     * @return 返回数据
     */
    public function getSeekDogDataDetail(){
        header("Access-Control-Allow-Origin:*");
        extract(input());
        $data = Db::table('up_dog')
            ->alias('ud')
            ->join('up_u_user uuu','ud.user_id=uuu.user_id','left')
            ->join('up_area ua','ud.area_shi=ua.district_id','left')
            ->join('up_area uaa','ud.area_qu=uaa.district_id','left')
            ->join('up_dog_family udf','ud.udf_id=udf.udf_id','left')
            ->join('up_dog_seek_characteristic uds','ud.dog_seek_characteristic=uds.udskc_id')
//            ->join('up_dog_miss_reason umr','ud.dog_seek_so=umr.reason_id')
            ->where(['dog_id'=>$dog_id])
            ->field('uuu.user_id,uuu.user_username,uuu.user_image,uuu.user_integral,uuu.user_attestation,ua.district shi,uaa.district qu,ud.dog_release_time,udf.udf_name,ud.dog_seek_time,ud.dog_add,ud.dog_seek_status,ud.dog_information,ud.dog_img,ud.dog_seek_people,ud.dog_seek_phone,ud.dog_seek_money_xx,ud.dog_seek_money_ok,ud.dog_seek_so,ud.dog_seek_so_qita,ud.dog_attention_num,uds.udskc_habitus,uds.udskc_hair,uds.udskc_sex,uds.udskc_tail,uds.udskca_id,uds.udskca_id_other,uds.udskci_id,uds.udskci_id_other')
            ->find();

        if($data['dog_img']){
            $img = trim($data['dog_img'],',');
            $data['dog_img'] = explode(',',$img);
        }
        if($data['udskca_id'] !=="" && $data['udskci_id'] !==""){
            $b = Db::table('up_dog_seek_characteristic_apparel')
                ->where('udskca_id','IN',$data['udskca_id'])
                ->column('udskca_name');
            $a = Db::table('up_dog_seek_characteristic_other')
                ->where('udsci_id','IN',$data['udskci_id'])
                ->column('udsci_name');
            $b_num = explode(',',$data['udskca_id']);
            $a_num = explode(',',$data['udskci_id']);
            if(in_array(0,$b_num)){
                $b['other'] = $data['udskca_id_other'];
            }
            if(in_array(0,$a_num)){
                $a['other'] = $data['udskci_id_other'];
            }

            $data['apparel'] = $b;
            $data['others'] = $a;
        }

        if($data['dog_seek_so'] == 0){
            $data['reason_name'] = $data['dog_seek_so_qita'];
        }else{
            $data['reason_name'] = Db::table('up_dog_miss_reason')->where('reason_id',$data['dog_seek_so'])->value('reason_name');
        }
        switch($data['dog_seek_status'])
        {
            case 1:
                $data['dog_seek_status'] = "寻找中";
                break;
            case 2:
                $data['dog_seek_status'] = "已找回";
                break;
            default:
                $data['dog_seek_status'] = "暂停寻找";
        }
        switch($data['udskc_habitus'])
        {
            case 1:
                $data['udskc_habitus'] = "小型狗";
                break;
            case 2:
                $data['udskc_habitus'] = "中型狗";
                break;
            default:
                $data['udskc_habitus'] = "大型狗";
        }
        switch($data['udskc_hair'])
        {
            case 1:
                $data['udskc_hair'] = "长毛";
                break;
            default:
                $data['udskc_hair'] = "短毛";
        }
        switch($data['udskc_tail'])
        {
            case 1:
                $data['udskc_tail'] = "长尾";
                break;
            case 2:
                $data['udskc_tail'] = "短尾";
                break;
            default:
                $data['udskc_tail'] = "无尾巴";
        }
        switch($data['udskc_sex'])
        {
            case 1:
                $data['udskc_sex'] = "公";
                break;
            default:
                $data['udskc_sex'] = "母";
        }
        return json_encode(['status'=>200,'msg'=>$data]);

    }

}